//
//  ABNavigationControllerViewController.swift
//  ABNavigation
//
//  Created by FaiWong on 2023/2/26.
//

import UIKit
import ABExtension
let kScreenWidth: CGFloat = UIScreen.main.bounds.size.width
let kScreenHeight: CGFloat = UIScreen.main.bounds.size.height
let kStatusBarHeight: CGFloat = {
    if #available(iOS 13.0, *) {
        guard let statusBarHeight = UIApplication.getTopWindow()?.windowScene?.statusBarManager?.statusBarFrame.size.height, statusBarHeight != 0 else { return 0 }
        return statusBarHeight
    } else {
        return UIApplication.shared.statusBarFrame.size.height
    }
}()
public class ABNavigationControllerDelegator: NSObject, UINavigationControllerDelegate {
    weak var navigation:UINavigationController?
    public func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
        self.updateNavStatus(viewController: viewController)
        self.navigation?.setNavigationBarHidden(viewController.isVisableNavigationBar==false, animated: true)
    }
    
    public func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
        self.navigation?.interactivePopGestureRecognizer?.delegate = nil
        self.navigation?.interactivePopGestureRecognizer?.isEnabled = viewController.isEnablePopGesture
        if navigationController.children.count <= 1 {
            self.navigation?.interactivePopGestureRecognizer?.isEnabled = false
        }
        self.navigation?.setNavigationBarHidden(viewController.isVisableNavigationBar==false, animated: false)
        self.updateNavStatus(viewController: viewController)
    }
    
    public func updateNavStatus(viewController:UIViewController) {
        let childrenCount = self.navigation?.children.count ?? 0
        if childrenCount <= 1 {return}
        if let vc = viewController as? ABViewController {
            if #available(iOS 13.0, *) {
                if vc.statusBarStyle == .darkContent {
                    self.setBlackBack(viewController: viewController)
                }else {
                    self.setWhiteBack(viewController: viewController)
                }
            } else {
                if vc.statusBarStyle == .default {
                    self.setBlackBack(viewController: viewController)
                }else {
                    self.setWhiteBack(viewController: viewController)
                }
            }
            return
        }
        self.setBlackBack(viewController: viewController)
    }
    
    public func setBlackBack(viewController:UIViewController) {
        let btn = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
        btn.setImage(UIImage(named: "back"), for: .normal)
        btn.addTarget(self, action: #selector(onBack), for: .touchUpInside)
        viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btn)
        
        if let vc = viewController as? ABViewController {
            vc.customNav?.refreshToBlackStyle()
        }
    }
    
    public func setWhiteBack(viewController:UIViewController) {
        let btn = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
        btn.setImage(UIImage(named: "back"), for: .normal)
        btn.addTarget(self, action: #selector(onBack), for: .touchUpInside)
        viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btn)
        
        if let vc = viewController as? ABViewController {
            vc.customNav?.refreshToWhiteStyle()
        }
    }
    
    @objc public func onBack() {
        self.navigation?.popViewController(animated: true)
    }
    
}

public class ABNavigationController: UINavigationController {
    public var delegator = ABNavigationControllerDelegator()
    public override var childForStatusBarStyle: UIViewController? {
        return visibleViewController
    }

    open override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        get {
            return [.portrait]
        }
    }
    
    
    open override var shouldAutorotate: Bool {
        return false
    }
    
    public override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.view.backgroundColor = UIColor(hexStr: "f3f3f2")
        modalPresentationStyle = .fullScreen
        
        self.delegator.navigation = self
        self.delegate = self.delegator
        self.navigationBar.shadowImage = UIImage()
        
        UINavigationBar.appearance().shadowImage = UIImage(color: UIColor.white)
        UINavigationBar.appearance().setBackgroundImage(UIImage(color: UIColor.white), for: .default)
        
        // iOS15-导航栏问题的处理
        if #available(iOS 15.0, *) {
            let navBarAppearance = UINavigationBarAppearance()
            navBarAppearance.configureWithOpaqueBackground()
            navBarAppearance.backgroundColor = .white
            navBarAppearance.shadowImage = UIImage(color: UIColor.white)
            navBarAppearance.backgroundImage = UIImage(color: UIColor.white)
            //
            navigationBar.standardAppearance = navBarAppearance
            navigationBar.scrollEdgeAppearance = navBarAppearance
            navigationBar.layer.shadowColor = UIColor.black.withAlphaComponent(0.2).cgColor
            navigationBar.layer.shadowOffset = CGSize(width: 0, height: 3)
            navigationBar.layer.shadowOpacity = 0.2
            navigationBar.layer.shadowRadius = 20.0
//            navigationBar.scrollEdgeAppearance = navigationBar.standardAppearance
        }
    }
    
    public override var preferredStatusBarStyle: UIStatusBarStyle {
        if let vc = self.topViewController {
            return vc.preferredStatusBarStyle
        }
        return UIStatusBarStyle.default
    }
    
    public override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
    }
    
    public override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    }
    
    public override var prefersStatusBarHidden: Bool {
        return self.topViewController?.prefersStatusBarHidden ?? false
    }
}
